<?php
/**
 * If you are not authorized to modify this file, do NOT touch it.
 * @file global.func.php
 * @author SkyWorld
 * @date 2011-9-12
 * @description This file contains all the global functions of the framework
 */

if(!defined('IN_SKY')) exit ('Access Denied.');

    /**
	 * @name strip_sql
	 * @author SkyWorld
	 * @date 2011-9-12
	 * @description this function to call will strip the dangerous words of sql
     * @parameters  $string: string to be strip off, the string can also be an array
     * @return $string: also can be an array
     **/
function strip_sql($string)
{
	global $search_arr,$replace_arr;
	return is_array($string) ? array_map('strip_sql',$string) : preg_replace($search_arr,$replace_arr,$string);
}

    /**
	 * @name new_addslashes
	 * @author SkyWorld
	 * @date 2011-9-12
	 * @description this function to call will strip the dangerous words of sql
     * @parameters  $string: string to be add slashes, and $string also can be array
     * @return $string: also can be an array
     **/
function new_addslashes($string)
{
	if(!is_array($string)) return addslashes($string);
	foreach($string as $key => $val)
		$string[$key] = new_addslashes($string);
	return $string;
}


    /**
	 * @name tplrefresh
	 * @author SkyWorld
	 * @date 2011-9-12
	 * @description To judge wether the template need to be refreshed
	 * 				If need refreshing return true, otherwise return false
     * @parameters String $comFile compiled file's path
     * 			   String $tplFile template file's path
     * @return boolen  If the template need to be refresh return true, otherwise return false
     **/
function tplrefresh($comFile,$tplFile){
	if(!is_readable($comFile))
		return true;
	if(@filemtime($comFile) > @filemtime($tplFile))
		return false;
	else
		return true;
}

    /**
	 * @name tplrefresh
	 * @author SkyWorld
	 * @date 2011-9-17
	 * @description This funtion used to parse templates
	 * 				If teplates has been complied return the complied filename
	 * 				Otherwise complie the templates, and then return the complied filename
     * @parameters  void
     * @return bool
     **/
function template($fileName){

	$fileName = trim($fileName);
	$fileDir = dirname($fileName)=='.' || dirname($fileName)=='/' ? '' : dirname($fileName).'/';
	$tplFile = SKY_TEMPLATES.$fileName;
	$comFile = SKY_TEMPLATES_C.$fileDir.'com_'.basename($fileName).'.php';
	$templates_c_dir = dirname($comFile);

	if (is_readable($comFile) && !tplrefresh($comFile,$tplFile)){
		return $comFile;
	}

	$temp="<?php if (!defined('IN_SKY')) exit ('Access Denied.'); ?>\n";
	$temp .=@file_get_contents($tplFile);

	/*patterns array*/
	if(file_exists($tplFile))
	{
	$pattern = array(
		'/\{\s*template\s*(.+?)\s*\}/',
		'/\{\s*\$([\w\x7f-\xff][\w\x7f-\xff\[\]\"\']*)\s*\}/i',
		'/<!--\s*if\s*(.+?)\s*-->/',
		'/<!--\s*else\s*-->/',
		'/<!--\s*else\s*if\s*(.+?)\s*-->/',
		'/<!--\s*\/if\s*-->/',
		'/<!--\s*loop\s+\\$(.+?)\s+\\$(.+?)\s+\\$(.+?)\s*-->/',
		'/<!--\s*loop\s+\\$(.+?)\s+\\$(.+?)\s*-->/',
		'/<!--\s*\/loop\s*-->/',
		'/\{\s*([A-Z_]+?)\s*\}/',
		'/\{\s*lang\s+(\w+?)\s*\}/',
		'/\{\s*lang\s+(\w+?)\s+(\w+?)\s*\}/',
		'/\{\s*php\s+(.+?)\s*\}/'
	);

	/*replacements array*/
	$replacement = array(
		'<?php include template ${1} ?>',
		'<?php echo $${1}; ?>',
		'<?php if ${1} { ?>',
		'<?php } else { ?>',
		'<?php } elseif ${1} { ?>',
		'<?php } ?>',
		'<?php foreach ($${1} as $${2} => $${3}) { ?>',
		'<?php foreach ($${1} as $${2}) { ?>',
		'<?php } ?>',
		'<?php echo ${1}; ?>',
		'<?php echo \$lang[\'${1}\']; ?>',
		'<?php echo \$lang[\'${1}\'][\'${2}\']; ?>',
		'<?php ${1}?>'
	);
	$temp = preg_replace($pattern,$replacement,$temp);
	$temp = trim($temp);

	if (!file_exists($templates_c_dir))
	{
		@mkdir($templates_c_dir, 0777, true) or die("File dir can not be created!");
	}
	file_put_contents($comFile, $temp);
	}else{
		die('Template file cannot be found.');
	}
	return $comFile;
}
?>
